﻿using Furion.DependencyInjection;
using LS.Nc6.Application.System;
using LS.Nc6.Core;
using LS.Nc6.Web.Entry.ViewModel.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections;


namespace LS.Nc6.Web.Entry.Controllers
{
    public class ContentUIController : Controller
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ICmsContentService _contentService;
        private readonly ISysFileService _fileService;
        private  readonly ICache _cache;

        
        public ContentUIController(ICmsChannelService cmsChannelService,
            ISysFileService fileService,
            ICmsContentService contentService, 
            IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed) 
        {
            _cmsChannelService = cmsChannelService;
            _contentService = contentService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        /// <summary>
        /// UI首页
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Index(long id)
        {
            var contentmodel = await _contentService.GetByIdAsync(id);
            if (contentmodel == null) return RedirectToAction("NotFound", "Home");

            var channelmodel = await _cmsChannelService.GetByIdAsync(contentmodel.ChannelId);
            ViewBag.Title = channelmodel.ChannelName;

            CmsContentVm cmsContentVm = new CmsContentVm();
            cmsContentVm.ChildChanName = channelmodel.ChannelName;
            cmsContentVm.ChanIndex = channelmodel.IndexName;
            if (channelmodel.ParentId != 1)
            {
                var pchannelmodel = await _cmsChannelService.GetByIdAsync(channelmodel.ParentId.Value);
                cmsContentVm.ChanName = pchannelmodel.ChannelName;
            }
            else
            {
                cmsContentVm.ChanName = channelmodel.ChannelName;
            }
            
            var pervcontent= await _contentService.GetFirstModelAsync(a => a.Id < id && a.ChannelId== contentmodel.ChannelId, a => new { a.Id }, true);
            cmsContentVm.PrevId = pervcontent?.Id;
            cmsContentVm.PrevTitle= pervcontent?.Title;
            var nextcontent = await _contentService.GetFirstModelAsync(a => a.Id > id && a.ChannelId == contentmodel.ChannelId, a => new { a.Id }, false);
            cmsContentVm.NextId = nextcontent?.Id;
            cmsContentVm.NextTitle = nextcontent?.Title;

            cmsContentVm.ChanBackground = !string.IsNullOrEmpty(channelmodel.ImageUrlIds) ? (await _fileService.GetByIdAsync(CommonUtil.GetLongList(channelmodel.ImageUrlIds)[0]))?.Fpath : "ui/image/channlbg.jpg";
            var cmschanlist = await _cmsChannelService.List(a => a.ParentId == channelmodel.Id, a => a.Taxis, a => new { a.Id, a.ChannelName, a.IndexName });

            cmsContentVm.Id = contentmodel.Id;
            cmsContentVm.Title = contentmodel.Title;
            cmsContentVm.SubTitle = contentmodel.SubTitle;
            cmsContentVm.Summary =contentmodel.Summary;
            cmsContentVm.AddTime = contentmodel.AddTime.ToString("yyyy-MM-dd");
            cmsContentVm.Body = contentmodel.Body;
            return View(cmsContentVm);
        }
      


        #region 公有方法


        /// <summary>
        /// 获取分类文章数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetChannelNum()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 6; i++)
            {
                Hashtable hs = new Hashtable ();
                hs.Add("text","分类");
                hs.Add("value", 100 * (i + 1) + new System.Random().Next(1, 99));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        /// <summary>
        /// 获取分类文章排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetRankList()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 15; i++)
            {
                Hashtable hs = new Hashtable();
                hs.Add("no",i+1);
                hs.Add("title", "测试新闻测试新闻测试新闻测试新闻");
                hs.Add("datetime", DateTime.Now.ToString("yy-MM-dd"));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        [HttpGet]
        public ResponseDataResult GetBrokenLine()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            List<string> xAxis = new List<string>();
            List<int> yAxis = new List<int>();
            for (int i = 0; i < 30; i++)
            {
                xAxis.Add((i + 1)+"日");
                yAxis.Add(100 * (i + 1) + new System.Random().Next(1, 99));
            }
            dr.data = new { xAxis, yAxis };
            return dr;
        }


        #endregion

        #region 私有方法
        private void GetChildren(NavItemVM etm, List<CmsChannelEntity> listpartmenu, long pid)
        {
            var children = listpartmenu.Where(w => w.ParentId == pid);
            List<NavItemVM> lsetm = new List<NavItemVM>();
            if (children != null && children.Count() > 0)
            {
                foreach (var item in children)
                {
                    NavItemVM etmchildren = new NavItemVM();
                    etm.Id = item.Id;
                    etm.NavName = item.ChannelName;
                    etm.NavIndex = item.IndexName;
                    GetChildren(etmchildren, listpartmenu, item.Id);
                    lsetm.Add(etmchildren);
                }
                if (lsetm.Count > 0)
                {
                    etm.Children = lsetm;
                }
            }
        }
        #endregion

    }
}